package com.serwer.gui;

import java.awt.BorderLayout;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Time;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.m.notowania.*;
import com.m.data.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;
import java.awt.SystemColor;
import javax.swing.ImageIcon;

public class mainPanel extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTextField jTextField1;
	private JTextField jTextField2;
	private JTextField jTextField3;
	private JTable table_1;
	private JTextField jTextField4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					String admin = null;
					mainPanel frame = new mainPanel(admin);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public mainPanel(final String admin) {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 583, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("mSerwer");
		label.setForeground(new Color(0, 128, 128));
		label.setFont(new Font("Tahoma", Font.BOLD, 26));
		label.setBounds(435, 11, 122, 23);
		contentPane.add(label);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Opcje", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(371, 54, 186, 149);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		final JCheckBox chckbxZapis = new JCheckBox("zapis do bazy");
		chckbxZapis.setBounds(31, 24, 97, 23);
		panel_2.add(chckbxZapis);
		chckbxZapis.setFont(new Font("Tahoma", Font.PLAIN, 11));
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Zarz\u0105dzaj notowaniami", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 54, 351, 396);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setToolTipText("");
		scrollPane.setBounds(10, 21, 330, 136);
		panel.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Data", "Index", "Warto\u015B\u0107", "+/-"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane.setViewportView(table);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Nowe dane", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 168, 330, 217);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("Data");
		lblNewLabel_1.setBounds(10, 27, 46, 14);
		panel_1.add(lblNewLabel_1);
		
		jTextField1 = new JTextField();
		jTextField1.setBounds(66, 24, 86, 20);
		panel_1.add(jTextField1);
		jTextField1.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Index");
		lblNewLabel_2.setBounds(10, 120, 46, 14);
		panel_1.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("Wartosc");
		lblNewLabel_3.setBounds(10, 151, 58, 14);
		panel_1.add(lblNewLabel_3);
		
		JButton btnNewButton = new JButton("Teraz");
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 
				Calendar cal = new GregorianCalendar();
				    Date creationDate = cal.getTime();
				    SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				    System.out.println(date_format.format(creationDate));
				jTextField1.setText(date_format.format(creationDate));
				
			}
		});
		btnNewButton.setBounds(10, 54, 142, 23);
		panel_1.add(btnNewButton);
		
		jTextField2 = new JTextField();
		jTextField2.setBounds(66, 117, 86, 20);
		panel_1.add(jTextField2);
		jTextField2.setColumns(10);
		
		/*wysylanie danych*/
		JButton btnNewButton_1 = new JButton("WYSLIJ");
		btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				           	
				
				 String queueName = "notowania.queue";		         
			        Producer producer;       	             
			        producer = new Producer();       

			            NotowaniaData notow = new NotowaniaData();
			            String Data = jTextField1.getText();
			            String id_ind = jTextField4.getText();
			            String ind = jTextField2.getText();
			            String wart = jTextField3.getText();
			            String LokStary;
			            LokStary = getLocalNastepny();
			            setNowy(wart);
			            setLocalNastepny(wart);
			            
			            String WartoscStara = LokStary;
			            
			            String Kurs = null;
		                
		                if (WartoscStara == null)
		                	WartoscStara = "0";
		                else
		                {			                			                
		                Double dwart = Double.parseDouble(wart);
		                Double dWartoscStara = Double.parseDouble(WartoscStara);
		                
		                if(wart.equals(WartoscStara))
		                	Kurs = "0";
		                else if(dwart < dWartoscStara)
		                	Kurs = "-";
		                else if(dwart > dWartoscStara)
		                	Kurs = "+";
		                }
			            
			            
			            DefaultTableModel model=(DefaultTableModel) table.getModel();
		                model.addRow(new Object []{				
		    					Data,ind,wart,Kurs
		    	         });  
			         
			            System.out.println(Data);
			            notow.setData(Data);
			            notow.setIndex(ind);
			            
			            
			            String stary;
			            stary = getNastepny();
			            notow.setWartosc(wart);
			            setNastepny(wart);
			            notow.setWartoscStara(stary);
			           /* 
			            String wart = jTextField3.getText();		            
			            String stary;	            
			            stary = getNastepny();
			            p1.setNowy(wart);
			            setNastepny(wart);
			            p1.setStary(stary);
			 
			           */ 
			            producer.produceMessage(queueName, notow);
			            
			            Boolean auto_zapis = chckbxZapis.isSelected();
		                
			            System.out.println(auto_zapis);
			           if(auto_zapis == true)
			           {
			        	   System.out.println("zapis");
			        	   String inSql="";
			               
			               try{
			                  Class.forName("com.mysql.jdbc.Driver");
			                   Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
			          Statement stmt=con.createStatement();
			                inSql="INSERT INTO notow(data, wartosc, id_index) VALUES"
			                		+ "("+quotate(Data)+","+quotate(wart)+","+quotate(id_ind)+")";
			                  //System.out.println(inSql);
			                  int done=stmt.executeUpdate(inSql);
			             
			              }
			              catch(Exception ew){
			               //   JOptionPane.showMessageDialog(this, e.getMessage());
			              }
			           }
			           else
			           {
			        	   System.out.println("brak zapisu");
			           }
     
			}
		});
		
		jTextField3 = new JTextField();
		jTextField3.setBounds(66, 148, 86, 20);
		panel_1.add(jTextField3);
		jTextField3.setColumns(10);
		
		
		btnNewButton_1.setBounds(10, 179, 142, 20);
		panel_1.add(btnNewButton_1);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(162, 20, 158, 124);
		panel_1.add(scrollPane_1);
		
		table_1 = new JTable();
		table_1.setShowVerticalLines(false);
		table_1.setShowHorizontalLines(false);
		table_1.setShowGrid(false);
		scrollPane_1.setViewportView(table_1);
		table_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
		            int row = table_1.getSelectedRow();
		            String Table_click = (table_1.getModel().getValueAt(row, 1).toString());
		            String Table_click_id = (table_1.getModel().getValueAt(row, 0).toString());
		            jTextField2.setText(Table_click);	
		            jTextField4.setText(Table_click_id);
			}
			
		});
		table_1.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				 DefaultTableModel model=(DefaultTableModel) table_1.getModel();
			        //czyszczenie
			        model.setRowCount(0);
			        
			      //  model.fireTableDataChanged();
			        String sql="SELECT * FROM Indexy";			                 
			        try {
			            
			            Class.forName("com.mysql.jdbc.Driver");
			            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
			            /*As we are creating a connection on a local computer we will write the url as jdbc:mysql://localhost:3306 */
			            Statement stmt=con.createStatement();
			            ResultSet rs = stmt.executeQuery(sql);

			            while (rs.next())
			            {
			               String id = rs.getString("id_index");
			                String nazwa= rs.getString("index_name");

			                model.addRow(new Object []{
			                   id, nazwa
			                });			    
			            }
			        }
			        catch(Exception e) {
			         //   JOptionPane.showMessageDialog(this, e.getMessage());
			        }
			}
		});
		table_1.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "Indexy"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table_1.getColumnModel().getColumn(0).setResizable(false);
		table_1.getColumnModel().getColumn(0).setPreferredWidth(34);
		table_1.getColumnModel().getColumn(1).setResizable(false);
		
		JButton btnNewButton_2 = new JButton("Odswie\u017C");
		btnNewButton_2.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_2.setBounds(190, 155, 104, 23);
		panel_1.add(btnNewButton_2);
		
		jTextField4 = new JTextField();
		jTextField4.setBounds(66, 86, 86, 20);
		panel_1.add(jTextField4);
		jTextField4.setColumns(10);
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				 DefaultTableModel model=(DefaultTableModel) table_1.getModel();
			        //czyszczenie
			        model.setRowCount(0);
			        
			      //  model.fireTableDataChanged();
			        String sql="SELECT * FROM Indexy";

			                 
			        try {
			            
			            Class.forName("com.mysql.jdbc.Driver");
			            Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
			            /*As we are creating a connection on a local computer we will write the url as jdbc:mysql://localhost:3306 */
			            Statement stmt=con.createStatement();
			            ResultSet rs = stmt.executeQuery(sql);

			            while (rs.next())
			            {
			            	String id = rs.getString("id_index");
			                String nazwa= rs.getString("index_name");

			                model.addRow(new Object []{
			                   id, nazwa
			                });
			    
			            }
			        }
			        catch(Exception ee) {
			         //   JOptionPane.showMessageDialog(this, e.getMessage());
			        }
			}
		});
		
		JLabel lblNewLabel = new JLabel("Zalogowany jako:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel.setBounds(10, 448, 97, 13);
		contentPane.add(lblNewLabel);
		
		
		
		JButton btnNewButton_3 = new JButton("U\u017Cytkownicy");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Uzytkownicy uzytk = new Uzytkownicy(admin);
				uzytk.setVisible(true);
			}
		});
		btnNewButton_3.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_3.setBounds(10, 66, 126, 23);
		panel_2.add(btnNewButton_3);
		
		JButton btnNewButton_5 = new JButton("Archiwum");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Archiwum arch = new Archiwum(admin);
				arch.setVisible(true);
			}
		});
		btnNewButton_5.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_5.setBounds(10, 100, 166, 23);
		panel_2.add(btnNewButton_5);
		
		
		
		JPanel panel_3 = new JPanel();
		panel_3.setBounds(371, 340, 186, 97);
		contentPane.add(panel_3);
		panel_3.setLayout(null);
		
		JButton btnNewButton_4 = new JButton("Zamknij program");
		btnNewButton_4.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnNewButton_4.setBounds(10, 28, 166, 23);
		panel_3.add(btnNewButton_4);
		
		JLabel label_5 = new JLabel("New label");
		label_5.setFont(new Font("Tahoma", Font.PLAIN, 11));
		label_5.setBounds(101, 447, 80, 14);
		contentPane.add(label_5);
		label_5.setText(admin);
		
		JLabel lblGlwneOkno = new JLabel("Gl\u00F3wne okno");
		lblGlwneOkno.setForeground(SystemColor.textHighlight);
		lblGlwneOkno.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblGlwneOkno.setBounds(10, 21, 147, 19);
		contentPane.add(lblGlwneOkno);
		
		JLabel lblNewLabel_4 = new JLabel("");
		lblNewLabel_4.setIcon(new ImageIcon("C:\\Users\\marmicc\\Desktop\\PRO\\Broker\\ico\\server.png"));
		lblNewLabel_4.setBounds(371, 248, 46, 48);
		contentPane.add(lblNewLabel_4);
	}
	 public String quotate(String content){
		    
	        return "'"+content+"'";
	    }
	 
	 String nastepny;
	    public String getNastepny() {
	        return nastepny;
	    }
	    public void setNastepny(String nastepny) {
	        this.nastepny = nastepny;
	    }
	    ////
	    
	    String LocalNastepny, stary,nowy;
	    public String getLocalNastepny() {
	        return LocalNastepny;
	    }
	    public void setLocalNastepny(String LocalNastepny) {
	        this.LocalNastepny = LocalNastepny;
	    }
	    
	    public String getStary() {
	        return stary;
	    }
	    public void setStary(String stary) {
	        this.stary = stary;
	    }
	    
	    public String getNowy() {
	        return nowy;
	    }
	    public void setNowy(String nowy) {
	        this.nowy = nowy;
	    }
	    
}
